// mypage/myBaby/myBaby.js
var app=getApp();
var util=require("../../utils/util");
var my=require("../../api/my");

Page({
  /*页面的初始数据*/
  data: {
    uid: '',
    babys:[]
  },
  /*生命周期函数--监听页面加载*/
  onLoad: function (options) {
    var that = this;
    var uid=options.id;
    that.setData({uid: uid})
    //
    that.get_baby_list(uid)
  },
  // 修改/删除
  nav_edit:function(e){
    var id=e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../editBaby/editBaby?id='+id,
    })
  },
  // 添加
  add_baby:function(){
    var that=this,uid=that.data.uid;
    wx.navigateTo({
      url: '../addBady/addBaby?id='+uid,
    })
  },
  onShow:function(){
    var that=this,uid=that.data.uid;
    if(app.globalData.change_baby){
      that.get_baby_list(uid);
      app.globalData.change_baby=false;
    }
  },
  // 获取宝宝列表
  get_baby_list:function(uid){
    wx.showLoading({title:"加载中"})
    var that=this;
    my.get_baby_list({parentId:uid}).then(res=>{
      if(res.data.code==200){
        app.globalData.change_baby=false;
        var list=res.data.data;
        if(list.length > 0){
          for(var i=0;i<list.length;i++){
            list[i].birthday=util.formatTime_age(new Date(list[i].birthday))
            var num = (new Date().getFullYear() - new Date(list[i].birthday).getFullYear())>0?(new Date().getFullYear() - new Date(list[i].birthday).getFullYear()):1;
            list[i].age=util.convertToChinaNum(num)+'岁';
          }
        }
        that.setData({babys: list})
      }
    })
  },
})